package com.xb.p20_notification;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void notifi(View view){
        //1.获得通知管理器
        NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //2.建立一个通知。8.0后需要建立通知通道
        Notification notification=null;

        //8.0以上版本
        String id="mChannel";
        String name="通道1";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel mChannel=new NotificationChannel(id,name,NotificationManager.IMPORTANCE_LOW);//通知的重要程度
            Log.i("main",mChannel.toString());

            notificationManager.createNotificationChannel(mChannel);

            notification=new Notification.Builder(this,id)
                    .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),R.drawable.pink))
                    .setSmallIcon(R.drawable.ic_baseline_event_note_24)
                    .setContentTitle("通知标题")
                    .setContentText("通知内容")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    //3.绑定通知处理结果的Activity
                    .setContentIntent(PendingIntent.getActivity(this,1,new Intent(this,NotificationResult.class),PendingIntent.FLAG_CANCEL_CURRENT))
                    .build();
        }else{
            //4.0版本以上
            notification=new Notification.Builder(this)
                    .setContentTitle("通知标题")
                    .setContentText("通知内容")
                    //超大图标
                    .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(this.getResources(),R.drawable.pink)))
                    .setSmallIcon(R.drawable.ic_baseline_event_note_24)
                    .setOngoing(true)
                    .setContentIntent(PendingIntent.getActivity(this,1,new Intent(this,NotificationResult.class),PendingIntent.FLAG_CANCEL_CURRENT))
                    .build();
        }

        //4.发出通知
        notificationManager.notify(0,notification);

    }
}